package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.center.InnerOrderResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.beautifulStore.ProjectDetailActivity;

import java.math.BigDecimal;
import java.util.List;


import static android.R.attr.content;
import static android.R.attr.data;
import static android.R.attr.theme;
import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by Administrator on 2017/1/19.
 */

public class InnerOrderAdapter extends BaseAdapter {

    private Context mContext;
    private List<InnerOrderResult.InnerOrderData> list;
    private Handler handler;

    public InnerOrderAdapter(Context context, List<InnerOrderResult.InnerOrderData> data, Handler handler) {
        this.mContext = context;
        this.list = data;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_inner_order, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvMd = (TextView) convertView.findViewById(R.id.tvMd);
                holder.tvCharge = (TextView) convertView.findViewById(R.id.tvCharge);
                holder.tvDelete = (TextView) convertView.findViewById(R.id.tvDelete);
                holder.tvProject = (TextView) convertView.findViewById(R.id.tvProject);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            try {
                if (null != list.get(position) && !"".equals(list.get(position))) {
                    final InnerOrderResult.InnerOrderData data = list.get(position);

                    if (null != data.getThumbnail() && !"".equals(data.getThumbnail())) {  // diskCacheStrategy(DiskCacheStrategy.SOURCE)  原图缓存
                        Glide.with(mContext).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_eg).into(holder.ivImg);
                    }

                    if (null != data.getItemname() && !"".equals(data.getItemname())) {
                        holder.tvName.setText(data.getItemname());
                    } else {
                        holder.tvName.setText("");
                    }
                    if (null != data.getId() && !"".equals(data.getId())) {
                        holder.tvOrderId.setText("单号：" + data.getId());
                    } else {
                        holder.tvOrderId.setText("单号：");
                    }

                    if (null != data.getPaytime() && !"".equals(data.getPaytime())) {
                        holder.tvTime.setText("支付：" + data.getPaytime());
                    } else {
                        holder.tvTime.setText("支付：");
                    }

                    if (null != data.getInnerprice() && !"".equals(data.getInnerprice())) {
                        holder.tvProject.setText(data.getInnerprice() + "美豆");
                    } else {
                        holder.tvProject.setText("");
                    }
                    if (DataUtil.moreThanZero(data.getRedbeanprice()) && DataUtil.moreThanZero(data.getPrice())) {
                        holder.tvMd.setText("+" + data.getRedbeanprice() + "红豆");
                        holder.tvCharge.setText("+" + data.getPrice() + "元");
                    } else if (DataUtil.moreThanZero(data.getRedbeanprice()) && !DataUtil.moreThanZero(data.getPrice())) {
                        holder.tvMd.setText("+" + data.getRedbeanprice() + "红豆");
                        holder.tvCharge.setText("");
                    } else if (!DataUtil.moreThanZero(data.getRedbeanprice()) && DataUtil.moreThanZero(data.getPrice())) {
                        holder.tvMd.setText("+" + data.getPrice() + "元");
                        holder.tvMd.setTextColor(mContext.getResources().getColor(R.color.bg_yield));
                        holder.tvCharge.setText("");
                    } else {
                        holder.tvMd.setText("");
                        holder.tvCharge.setText("");
                    }

                    holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                          AlertDialog dialog =  new AlertDialog.Builder(mContext).setTitle("您确认执行此操作吗？")
                                    .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Message msg = Message.obtain();
                                            msg.what = 0;
                                            msg.obj = position;
                                            handler.sendMessage(msg);
                                            dialog.dismiss();
                                        }
                                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(mContext.getResources().getColor(R.color.bg_yield));
                            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(mContext.getResources().getColor(R.color.bg_yield));

                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }


    class ViewHolder {
        private ImageView ivImg;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvName;
        private TextView tvMd;
        private TextView tvCharge;
        private TextView tvDelete;
        private TextView tvProject;
    }
}
